﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DanhMuc_QuanHeXaHoi : System.Web.UI.Page
{
    SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            btnclose.Attributes.Add("onClick", "javascript:history.back(); return false;");//them su kien vao  button de quay ve trang truoc
            CatalogAccess.BindItemToDropDown(drlLyLuanChinhTri, 7);
            CatalogAccess.BindItemToDropDown(drlQuanLyNhaNuoc,  1002);
            CatalogAccess.BindItemToDropDown(drlThuongBinh,  1003);
            CatalogAccess.BindItemToDropDown(drlGiaDinhChinhSach,  1004);
            CatalogAccess.BindItemToDropDown(drlDanhHieu, 6);
            BindDataToControl();
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"),"gold",128);
        _dbDataContext _db = new _dbDataContext();
        bool success = CatalogAccess.QuanHeXaHoi(madoanvien, int.Parse(drlLyLuanChinhTri.SelectedValue.ToString()), int.Parse(drlQuanLyNhaNuoc.SelectedValue.ToString()),
            int.Parse(drlThuongBinh.SelectedValue.ToString()), int.Parse(drlGiaDinhChinhSach.SelectedValue.ToString()), txtSoTruong.Text, txtNgayVaoDuBi.Value,
            txtNgayVaoChinhThuc.Value, txtNgayThamGiaToChucXaHoi.Value, txtCongViec.Text, txtNgayNhapNgu.Value, txtNgayXuatNgu.Value, txtQuanHam.Text, int.Parse(drlDanhHieu.SelectedValue.ToString()),
            txtNgayNhanDanhHieu.Value, txtKhenThuong.Text, txtNgayNhanKhenThuong.Value, txtKyLuat.Text, txtNgayNhanKhenThuong.Value);
        if (success)
        {
            StatusLabel.Text = "Cập nhật thành công.";
            StatusLabel.Visible = true;
        }
        else
        {
            StatusLabel.Visible = true;
            StatusLabel.Text = "Không lưu được.";
        }
    }
    private void BindDataToControl()
    {
        string madoanvien = _ps.Decrypt(Request.QueryString["codeunionists"].Replace(" ", "+"),"gold",128);
        _dbDataContext _db = new _dbDataContext();
        int count = _db.tblQuanHeXaHois.Where(r=>r.MaDoanVien.Trim()==madoanvien).Count();
        if (count != 0)
        {
            var val = _db.tblQuanHeXaHois.Single(r => r.MaDoanVien.Trim() == madoanvien);
            drlLyLuanChinhTri.SelectedValue = val.LyLuanChinhTriID.ToString();
            drlQuanLyNhaNuoc.SelectedValue = val.QuanLyNhaNuocID.ToString();
            drlThuongBinh.SelectedValue = val.ThuongBinhID.ToString();
            drlGiaDinhChinhSach.SelectedValue = val.GiaDinhChinhSachID.ToString();
            try
            {
                txtSoTruong.Text = val.SoTruong.Trim();
            }
            catch { }
            txtNgayVaoDuBi.Value = string.Format("{0:dd/MM/yyyy}",val.NgayVaoDangDuBi);
            txtNgayVaoChinhThuc.Value = string.Format("{0:dd/MM/yyyy}", val.NgayVaoDangChinhThuc);
            txtNgayThamGiaToChucXaHoi.Value = string.Format("{0:dd/MM/yyyy}", val.NgayThamGiaTCCTXH);
            try
            {
                txtCongViec.Text = val.CongViecTrongTCCTXH.Trim();
            }
            catch { }
            txtNgayNhapNgu.Value = string.Format("{0:dd/MM/yyyy}", val.NgayNhapNgu);
            txtNgayXuatNgu.Value = string.Format("{0:dd/MM/yyyy}", val.NgayXuatNgu);
            try
            {
                txtQuanHam.Text = val.QuanHam.Trim();
            }
            catch { }
            drlDanhHieu.SelectedValue = val.DanhHieuID.ToString();
            txtNgayNhanDanhHieu.Value = string.Format("{0:dd/MM/yyyy}", val.NgayNhanDanhHieu);
            try
            {
                txtKhenThuong.Text = val.KhenThuong.Trim();
            }
            catch { }
            txtNgayNhanKhenThuong.Value = string.Format("{0:dd/MM/yyyy}", val.NgayNhanKhenThuong);
            try
            {
                txtKyLuat.Text = val.KyLuat.Trim();
            }
            catch { }
            txtNgayKyLuat.Value = string.Format("{0:dd/MM/yyyy}", val.NgayBiKyLuat);
        }
    }
}